home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / misc / maplay1_2.lha / maplay / subband_layer_1.cc < prev    next >
C/C++ Source or Header  |  1994-06-23  |  8KB  |  269 lines

  1. /*
  2.  *  @(#) subband_layer_1.cc 1.7, last edit: 6/15/94 16:51:49
  3.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  4.  *  @(#) Berlin University of Technology
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  *  Changes from version 1.1 to 1.2:
  23.  *    - scalefactors itself instead of scalefactor indices are stored in
  24.  *      SubbandLayer1... objects
  25.  *    - check for small values in [-1.0E-7, 1.0E-7] removed, because the
  26.  *      test itself was slower than some SynthesisFilter::input_sample() calls
  27.  *    - check for illegal scalefactor index 63 removed
  28.  */
  29.  
  30. #include <stdlib.h>
  31. #include "subband_layer_1.h"
  32. #include "scalefactors.h"
  33.  
  34.  
  35. // factors and offsets for sample requantization:
  36. static const real table_factor[15] = {
  37.   0.0, (1.0/2.0) * (4.0/3.0), (1.0/4.0) * (8.0/7.0), (1.0/8.0) * (16.0/15.0),
  38.   (1.0/16.0) * (32.0/31.0), (1.0/32.0) * (64.0/63.0), (1.0/64.0) * (128.0/127.0),
  39.   (1.0/128.0) * (256.0/255.0), (1.0/256.0) * (512.0/511.0),
  40.   (1.0/512.0) * (1024.0/1023.0), (1.0/1024.0) * (2048.0/2047.0),
  41.   (1.0/2048.0) * (4096.0/4095.0), (1.0/4096.0) * (8192.0/8191.0),
  42.   (1.0/8192.0) * (16384.0/16383.0), (1.0/16384.0) * (32768.0/32767.0)
  43. };
  44.  
  45. static const real table_offset[15] = {
  46.   0.0, ((1.0/2.0)-1.0) * (4.0/3.0), ((1.0/4.0)-1.0) * (8.0/7.0), ((1.0/8.0)-1.0) * (16.0/15.0),
  47.   ((1.0/16.0)-1.0) * (32.0/31.0), ((1.0/32.0)-1.0) * (64.0/63.0), ((1.0/64.0)-1.0) * (128.0/127.0),
  48.   ((1.0/128.0)-1.0) * (256.0/255.0), ((1.0/256.0)-1.0) * (512.0/511.0),
  49.   ((1.0/512.0)-1.0) * (1024.0/1023.0), ((1.0/1024.0)-1.0) * (2048.0/2047.0),
  50.   ((1.0/2048.0)-1.0) * (4096.0/4095.0), ((1.0/4096.0)-1.0) * (8192.0/8191.0),
  51.   ((1.0/8192.0)-1.0) * (16384.0/16383.0), ((1.0/16384.0)-1.0) * (32768.0/32767.0)
  52. };
  53.  
  54.  
  55.  
  56. /**********************/    // used for single channel mode
  57. /*** Standard Class ***/    // and in derived class for intensity
  58. /**********************/    // stereo mode
  59.  
  60. SubbandLayer1::SubbandLayer1 (uint32 subbandnumber)
  61. {
  62.   this->subbandnumber = subbandnumber;
  63.   samplenumber = 0;
  64. }
  65.  
  66.  
  67. void SubbandLayer1::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  68. {
  69.   if ((allocation = stream->get_bits (4)) == 15)
  70.     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  71.   if (crc)
  72.     crc->add_bits (allocation, 4);
  73.   if (allocation)
  74.   {
  75.     samplelength = allocation + 1;
  76.     factor = table_factor[allocation];
  77.     offset = table_offset[allocation];
  78.   }
  79. }
  80.  
  81.  
  82. void SubbandLayer1::read_scalefactor (Ibitstream *stream, Header *)
  83. {
  84.   if (allocation)
  85.     scalefactor = scalefactors[stream->get_bits (6)];
  86. }
  87.  
  88.  
  89. bool SubbandLayer1::read_sampledata (Ibitstream *stream)
  90. {
  91.   if (allocation)
  92.   {
  93.     sample = real (stream->get_bits (samplelength));
  94. #ifdef DEBUG
  95.     if (sample == (1 << samplelength) - 1)
  96.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  97. #endif
  98.   }
  99.   if (++samplenumber == 12)
  100.   {
  101.     samplenumber = 0;
  102.     return True;
  103.   }
  104.   return False;
  105. }
  106.  
  107.  
  108. bool SubbandLayer1::put_next_sample (e_channels channels,
  109.                      SynthesisFilter *filter1, SynthesisFilter *)
  110. {
  111.   if (allocation && channels != right)
  112.   {
  113.     register real scaled_sample = (sample * factor + offset) * scalefactor;
  114. #ifdef DEBUG
  115.     if (scaled_sample < -1.0 || scaled_sample > 1.0)
  116.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  117.       // this should never occur
  118. #endif
  119.     filter1->input_sample (scaled_sample, subbandnumber);
  120.   }
  121.   return True;
  122. }
  123.  
  124.  
  125. /******************************/
  126. /*** Intensity Stereo Class ***/
  127. /******************************/
  128.  
  129. SubbandLayer1IntensityStereo::SubbandLayer1IntensityStereo (uint32 subbandnumber)
  130. : SubbandLayer1 (subbandnumber)
  131. {
  132. }
  133.  
  134.  
  135. void SubbandLayer1IntensityStereo::read_scalefactor (Ibitstream *stream, Header *)
  136. {
  137.   if (allocation)
  138.   {
  139.     scalefactor = scalefactors[stream->get_bits (6)];
  140.     channel2_scalefactor = scalefactors[stream->get_bits (6)];
  141.   }
  142. }
  143.  
  144.  
  145. bool SubbandLayer1IntensityStereo::put_next_sample (e_channels channels,
  146.     SynthesisFilter *filter1, SynthesisFilter *filter2)
  147. {
  148.   if (allocation)
  149.   {
  150.     sample = sample * factor + offset;        // requantization
  151.     if (channels == both)
  152.     {
  153.       register real sample1 = sample * scalefactor,
  154.             sample2 = sample * channel2_scalefactor;
  155. #ifdef DEBUG
  156.       if (sample1 < -1.0 || sample1 > 1.0 || sample2 < -1.0 || sample2 > 1.0)
  157.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  158.     // this should never occur
  159. #endif
  160.       filter1->input_sample (sample1, subbandnumber);
  161.       filter2->input_sample (sample2, subbandnumber);
  162.     }
  163.     else if (channels == left)
  164.     {
  165.       register real sample1 = sample * scalefactor;
  166. #ifdef DEBUG
  167.       if (sample1 < -1.0 || sample1 > 1.0)
  168.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  169.     // this should never occur
  170. #endif
  171.       filter1->input_sample (sample1, subbandnumber);
  172.     }
  173.     else
  174.     {
  175.       register real sample2 = sample * channel2_scalefactor;
  176. #ifdef DEBUG
  177.       if (sample2 < -1.0 || sample2 > 1.0)
  178.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  179.     // this should never occur
  180. #endif
  181.       filter1->input_sample (sample2, subbandnumber);
  182.     }
  183.   }
  184.   return True;
  185. }
  186.  
  187.  
  188.  
  189. /********************/
  190. /*** Stereo Class ***/
  191. /********************/
  192.  
  193. SubbandLayer1Stereo::SubbandLayer1Stereo (uint32 subbandnumber)
  194. : SubbandLayer1 (subbandnumber)
  195. {
  196. }
  197.  
  198.  
  199. void SubbandLayer1Stereo::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  200. {
  201.   allocation = stream->get_bits (4);
  202.   channel2_allocation = stream->get_bits (4);
  203.   if (crc)
  204.   {
  205.     crc->add_bits (allocation, 4);
  206.     crc->add_bits (channel2_allocation, 4);
  207.   }
  208.   if (allocation == 15 || channel2_allocation == 15)
  209.     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  210.   if (allocation)
  211.   {
  212.     samplelength = allocation + 1;
  213.     factor = table_factor[allocation];
  214.     offset = table_offset[allocation];
  215.   }
  216.   if (channel2_allocation)
  217.   {
  218.     channel2_samplelength = channel2_allocation + 1;
  219.     channel2_factor = table_factor[channel2_allocation];
  220.     channel2_offset = table_offset[channel2_allocation];
  221.   }
  222. }
  223.  
  224.  
  225. void SubbandLayer1Stereo::read_scalefactor (Ibitstream *stream, Header *)
  226. {
  227.   if (allocation)
  228.     scalefactor = scalefactors[stream->get_bits (6)];
  229.   if (channel2_allocation)
  230.     channel2_scalefactor = scalefactors[stream->get_bits (6)];
  231. }
  232.  
  233.  
  234. bool SubbandLayer1Stereo::read_sampledata (Ibitstream *stream)
  235. {
  236.   bool returnvalue = SubbandLayer1::read_sampledata (stream);
  237.   if (channel2_allocation)
  238.   {
  239.     channel2_sample = real (stream->get_bits (channel2_samplelength));
  240. #ifdef DEBUG
  241.     if (channel2_sample == (1 << channel2_samplelength) - 1)
  242.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  243. #endif
  244.   }
  245.   return returnvalue;
  246. }
  247.  
  248.  
  249. bool SubbandLayer1Stereo::put_next_sample (e_channels channels,
  250.                        SynthesisFilter *filter1, SynthesisFilter *filter2)
  251. {
  252.   SubbandLayer1::put_next_sample (channels, filter1, filter2);
  253.   if (channel2_allocation && channels != left)
  254.   {
  255.     register float sample2 = (channel2_sample * channel2_factor + channel2_offset) *
  256.                  channel2_scalefactor;
  257. #ifdef DEBUG
  258.     if (sample2 < -1.0 || sample2 > 1.0)
  259.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  260.       // this should never occur
  261. #endif
  262.     if (channels == both)
  263.       filter2->input_sample (sample2, subbandnumber);
  264.     else
  265.       filter1->input_sample (sample2, subbandnumber);
  266.   }
  267.   return True;
  268. }
  269.